import time
import subprocess


def cmd(command):
    subp = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8")
    subp.wait(2)
    if subp.poll() == 0:
        print(subp.communicate()[1])
    else:
        print("失败")


def runcmd(command):
    ret = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8",
                         timeout=1)
    if ret.returncode == 0:
        print("success:", ret)
    else:
        print("error:", ret)


cmd("python -version")
cmd("exit 1")
runcmd(["dir", "/b"])  # 序列参数
runcmd("exit 1")  # 字符串参数

subprocess.call('dir /a', shell=True)
